package com.zhy.generator.codegen.mybatis.javarequest;
import com.zhy.generator.codegen.api.CommentGenerator;
import com.zhy.generator.codegen.dom.java.CompilationUnit;
import com.zhy.generator.codegen.dom.java.FullyQualifiedJavaType;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.dom.java.TopLevelClass;
import com.zhy.generator.codegen.mybatis.AbstractJavaGenerator;
import com.zhy.generator.codegen.mybatis.javarequest.elements.*;

import java.util.ArrayList;
import java.util.List;

import static com.zhy.generator.codegen.util.Messages.getString;

/**
 * @author 源
 */
public class JavaRequestGenerator extends AbstractJavaGenerator {
    @Override
    public List<CompilationUnit> getCompilationUnits() {
        progressCallback.startTask(getString("Progress.13", introspectedTable.getFullyQualifiedTable()));
        CommentGenerator commentGenerator = context.getCommentGenerator();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getRequestType());
        TopLevelClass tlc = new TopLevelClass(type);
        tlc.setVisibility(JavaVisibility.PUBLIC);
        commentGenerator.addJavaFileComment(tlc);

        String rootInterface = "java.io.Serializable";
        FullyQualifiedJavaType serializable = new FullyQualifiedJavaType(rootInterface);

        tlc.addSuperInterface(serializable);
        tlc.addImportedType(serializable);

        //serialVersionUID
        addSerialVersionUIDField(tlc);

        addPkParamInnerClass(tlc);
        addAddParamInnerClass(tlc);
        addEditParamInnerClass(tlc);
        addListParamInnerClass(tlc);

        List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
//        if (context.getPlugins().modelBaseRecordClassGenerated(tlc, introspectedTable)) {
            answer.add(tlc);
//        }
        return answer;
    }

    private void addListParamInnerClass(TopLevelClass tlc) {
        AbstractJavaRequestInnerClassGenerator innerClassGenerator = new ListParamInnerClassGenerator();
        initializeAndExecuteGenerator(innerClassGenerator, tlc);
    }

    private void addEditParamInnerClass(TopLevelClass tlc) {
        AbstractJavaRequestInnerClassGenerator innerClassGenerator = new EditParamInnerClassGenerator();
        initializeAndExecuteGenerator(innerClassGenerator, tlc);
    }

    private void addAddParamInnerClass(TopLevelClass tlc) {
        AbstractJavaRequestInnerClassGenerator innerClassGenerator = new AddParamInnerClassGenerator();
        initializeAndExecuteGenerator(innerClassGenerator, tlc);
    }

    private void addPkParamInnerClass(TopLevelClass tlc) {

        AbstractJavaRequestInnerClassGenerator innerClassGenerator = new PkParamInnerClassGenerator();
        initializeAndExecuteGenerator(innerClassGenerator, tlc);

    }

    protected void initializeAndExecuteGenerator(AbstractJavaRequestInnerClassGenerator methodGenerator, TopLevelClass tlc) {
        methodGenerator.setContext(context);
        methodGenerator.setIntrospectedTable(introspectedTable);
        methodGenerator.setProgressCallback(progressCallback);
        methodGenerator.setWarnings(warnings);
        methodGenerator.addInterfaceElements(tlc);
    }

}
